package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayQuestion;
import org.itboys.mobile.service.mongo.web.SundayQuestionService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/17
 * @Description：问答
 */
@Controller
@RequestMapping("/sunday/web/question")
public class SundayQuestionController extends BaseController {
    @Autowired
    private SundayQuestionService questionService;

    @RequestMapping("/index")
    public String index() {
        return "sunday/question/index";
    }

    @RequestMapping("/input")
    public String input(
            @RequestParam("answerId")Long answerId
            ,@RequestParam("parentId") Long parentId
            ,Model model) {
        SundayQuestion question = questionService.getById(parentId);
        SundayQuestion answer = questionService.findOne(answerId);
        model.addAttribute("question",question);
        model.addAttribute("answer",answer);
        model.addAttribute("parentId",parentId);
        return "sunday/question/input";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response) {
        try {
            PageResult<SundayQuestion> result = questionService.select(request, null);
            JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    @RequestMapping("/answerIndex")
    public String answerIndex(@RequestParam("parentId") Long parentId
    ,Model model) {
        model.addAttribute("parentId",parentId);
        return "sunday/question/answerIndex";
    }

    @RequestMapping("/getAnswers")
    public void getAnswers(
            HttpServletRequest request, HttpServletResponse response
            , @RequestParam("parentId") Long parentId) {
        try {
            PageResult<SundayQuestion> result = questionService.select(request, parentId);
            JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    @RequestMapping(value = "/save")
    public void save(HttpServletRequest request, HttpServletResponse response
                     ,@RequestParam("memberId")Long memberId
                 ,@RequestParam("content")String content
            ,@RequestParam("parentId")Long parentId
            ,@RequestParam("answerId")Long answerId){
        try {
            questionService.save(answerId,memberId,parentId,content);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/question/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id
    ) {
        try {
            questionService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/question/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    @RequestMapping("/setStatus")
    public void setStatus(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id
                          ,@RequestParam(value="status",required=true) Integer status
    ) {
        try {
            questionService.setStatus(id,status);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/question/setStatus------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}

